/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.itgrail.pilot.app.command.cmo.AddOkrPeriodCmd;
import com.itgrail.pilot.app.converter.OkrPeriodClientConverter;
import com.itgrail.pilot.app.converter.PeriodControlClientConverter;
import com.itgrail.pilot.app.util.LocalDateUtil;
import com.itgrail.pilot.domain.user.entity.OkrPeriodE;
import com.itgrail.pilot.domain.user.entity.PeriodControlE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.ResultData;

import java.time.LocalDateTime;


@CmdHandler
@Slf4j
public class AddOkrPeriodCmdExe implements CommandExecutorI<ResultData<Integer>, AddOkrPeriodCmd> {

    @Autowired
    private OkrPeriodClientConverter okrPeriodClientConverter;

    @Autowired
    private PeriodControlClientConverter periodControlClientConverter;

    @Override
    public ResultData execute(AddOkrPeriodCmd cmd) {
        ResultData resultData = ResultData.success(null);
        check(cmd);
        OkrPeriodE okrPeriodE = okrPeriodClientConverter.addClientToEntity(cmd);
        //处理周期时间 start_date
        StringBuilder sb = new StringBuilder();
        try {
            LocalDateTime startTime = LocalDateUtil.dateToLocalDateTime(cmd.getStartTime());
            LocalDateTime endTime = LocalDateTime.now();
            Integer period = Integer.valueOf(cmd.getPeriodLength());
            switch (cmd.getPeriodType()) {
                case 0:
                    //月份
                    endTime = startTime.plusMonths(period).plusDays(-1);
                    sb.append(startTime.getYear());
                    sb.append("年");
                    sb.append(startTime.getMonthValue());
                    sb.append("月");
                    sb.append("-");
                    sb.append(endTime.getYear());
                    sb.append("年");
                    sb.append(endTime.getMonthValue());
                    sb.append("月");
                    break;
                case 1:
                    //季度
                    endTime = startTime.plusMonths(period).plusDays(-1);
                    sb.append("第");
                    sb.append(cmd.getPeriodLength());
                    sb.append("季度OKR");
                    break;
                case 2:
                    //年
                    endTime = startTime.plusYears(Integer.valueOf(cmd.getPeriodLength()));
                    sb.append(startTime.getYear());
                    sb.append("年度OKR");
                    break;

            }
            okrPeriodE.setStartDate(sb.toString());
            okrPeriodE.setEndTime(LocalDateUtil.dateTimeToDate(endTime));
        } catch (Exception e) {
            log.warn("转换日期失败");
        }
        okrPeriodE.setExtInfo(JSON.toJSONString(cmd.getOkrPeriodButtonCmd()));
        int save = okrPeriodE.save();
        if (cmd.getAddPeriodControlCmd() != null) {
            PeriodControlE periodControlE = periodControlClientConverter.addClientToEntity(cmd.getAddPeriodControlCmd());
            periodControlE.save();
        }
        resultData.setData(save);
        return resultData;
    }

    protected boolean check(AddOkrPeriodCmd cmd) {
        Preconditions.checkNotNull(cmd, "cmd不能为null");
        return true;
    }

}
